package cn.com.dpc.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.dpc.bean.User;

public class DataService {
	public static void insertUser(Context context, User user) {
		UserTable userTable = new UserTable(context, null);
		SQLiteDatabase sql = userTable.getWritableDatabase();
		sql.execSQL(
				"insert into " + UserTable.tableName + "(" + userTable.IP + ","
						+ UserTable.USERID + "," + UserTable.PASSWORD
						+ ") values(?,?,?)",
				new Object[] { user.getIp(), user.getUserId(),
						user.getPassword() });
		sql.close();
	}

	public static void deleteUser(Context context) {
		UserTable userTable = new UserTable(context, null);
		SQLiteDatabase sql = userTable.getWritableDatabase();
		sql.execSQL("delete from " + UserTable.tableName);
		sql.close();
	}

	public static User queryUser(Context context) {
		UserTable userTable = new UserTable(context, null);
		SQLiteDatabase sql = userTable.getWritableDatabase();
		Cursor cursor = sql.query(UserTable.tableName, null, null, null, null,
				null, "");
		if (cursor.getCount() > 0) {
			User user = new User();
			int ipIndex = cursor.getColumnIndex(UserTable.IP);
			int userIdIndex = cursor.getColumnIndex(UserTable.USERID);
			int passwordIndex = cursor.getColumnIndex(UserTable.PASSWORD);
			for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor
					.moveToNext()) {
				user.setIp(cursor.getString(ipIndex));
				user.setUserId(cursor.getString(userIdIndex));
				user.setPassword(cursor.getString(passwordIndex));
			}
			cursor.close();
			sql.close();
			return user;
		} else {
			cursor.close();
			sql.close();
			return null;
		}
	}

	public static void insertData(Context context, String userId, String data) {
		DateTable dateTable = new DateTable(context, null);
		SQLiteDatabase sql = dateTable.getWritableDatabase();
		sql.execSQL("insert into " + DateTable.tableName + "("
				+ DateTable.USERID + "," + DateTable.DATA + ") values(?,?)",
				new Object[] { userId, data });
		sql.close();
	}

	public static void deleteData(Context context, String userId) {
		DateTable dateTable = new DateTable(context, null);
		SQLiteDatabase sql = dateTable.getWritableDatabase();
		sql.execSQL("delete from " + DateTable.tableName + " where "
				+ DateTable.USERID + "=?", new Object[] { userId });
		sql.close();
	}

	public static String queryData(Context context) {
		DateTable dateTable = new DateTable(context, null);
		SQLiteDatabase sql = dateTable.getWritableDatabase();
		Cursor cursor = sql.query(DateTable.tableName, null, null, null, null,
				null, "");
		String result = "";
		if (cursor.getCount() > 0) {
			int dataIndex = cursor.getColumnIndex(DateTable.DATA);
			for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor
					.moveToNext()) {
				result = cursor.getString(dataIndex);
				break;
			}
		}
		cursor.close();
		sql.close();
		return result;
	}
}
